﻿using System;
using System.Globalization;
using System.Net;
using LiveNet.Authentication;

namespace LiveNet
{
    public sealed class DefaultRequestCreator : IWebRequestCreator<DefaultAuthenticationDetails>
    {
        #region IWebRequestCreator Members

        public HttpWebRequest Create(Uri url, DefaultAuthenticationDetails authenticationDetails)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            switch (authenticationDetails.TokenType)
            {
                case AuthenticationToken.DomainAuthenticationToken:
                    {
                        request.Headers.Add("Authorization",
                                            string.Format(CultureInfo.InvariantCulture, "DomainAuthToken at=\"{0}\"", authenticationDetails.Token));
                        break;
                    }
                case AuthenticationToken.RelyingPartySuite:
                    {
                        request.Headers.Add("Authorization",
                                            string.Format(CultureInfo.InvariantCulture, "WLID1.0 t=\"{0}\"", authenticationDetails.Token));
                        break;
                    }
                case AuthenticationToken.DelegatedToken:
                    {
                        //request.Headers.Add("Accept-Encoding", "gzip, deflate");
                        //request.Headers.Add("Pragma", "No-Cache");
                        request.Headers.Add("Authorization", "DelegatedToken dt=\"" + authenticationDetails.Token + "\"");
                        break;
                    }
            }
            return request;
        }

        #endregion
    }
}
